//
//  XC StatusRetweetedView.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/3/5.
//  Copyright © 2016年 小陈. All rights reserved.
//

/// 被转发微博

import UIKit
import SnapKit
class XCStatusRetweetedView: UIView {
        required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var StatusRetweetedConstraint: Constraint?

    override init(frame: CGRect) {
        super.init(frame: frame)
     
        prepareUI()
  }
    
    var status:XCStatus?{
        didSet{
            /// 转发的内容
            let name = status?.user?.screen_name ?? "没有名称"
            let text = status?.text ?? "没有微博内容"
            
            retweeteContenLable.text = "@\(name):\(text)"
            /// 转发的图片
            StatusRetweetedConstraint?.uninstall()/// 删除约束
            if let _ = status?.storePictureURLs {
               retweetPictureView.status = status
                retweetPictureView.hidden = false
               
             self.snp_makeConstraints { (make) -> Void in
                     self.StatusRetweetedConstraint = make.bottom.equalTo(retweetPictureView.snp_bottom).offset(XCStatusCellMargin).constraint
                }
            }else{
                retweetPictureView.hidden = true
                self.snp_makeConstraints { (make) -> Void in
                    self.StatusRetweetedConstraint = make.bottom.equalTo(retweeteContenLable.snp_bottom).constraint
                }
            }
            /// 有配图的情况
            
            /// 没有配图的情况
        }
    }
    
    
    func prepareUI(){
        // 添加控件
        addSubview(retweetedBgButom)
        addSubview(retweeteContenLable)
        addSubview(retweetPictureView)
        //添加约束
        retweetedBgButom.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        retweeteContenLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self).offset(XCStatusCellMargin)
            make.trailing.equalTo(self).offset(-XCStatusCellMargin)
        }
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(XCStatusCellMargin)

            make.top.equalTo(retweeteContenLable.snp_bottom).offset(XCStatusCellMargin)
            
        }
        self.snp_makeConstraints { (make) -> Void in
          self.StatusRetweetedConstraint = make.bottom.equalTo(retweetPictureView.snp_bottom).constraint
        }
    }
    
    
    /// 懒加载
    
    /// 转发微薄的背景
    lazy var retweetedBgButom :UIButton = {
     
        let button = UIButton()
       button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)

        return button
    }()
    /// 转发微博的内容
    lazy var retweeteContenLable:UILabel = {
       let lable = UILabel(textColor: UIColor.darkGrayColor(), font: 15)

        lable.numberOfLines = 0
        
        
        return lable
    }()
        /// 被转发微博的配图
    lazy var retweetPictureView :XCStatusPictureView = XCStatusPictureView()
    
    
    
}
